<?php

/*
 * This file is part of SoftSite.
 *
 * (c) 2012 Steve Tredinnick
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 *
 * @package softsite
 * @author  Steve Tredinnick <steve.tredinnick@gmail.com>
 */

namespace Classes;

abstract class BaseModel
{
    protected $tableName;
    protected $db;
    protected $id;

    public function __construct()
    {
        require_once('lib/ezSQL/shared/ez_sql_core.php');
        require_once('lib/ezSQL/mysql/ez_sql_mysql.php');

        // Initialise database object and establish a connection
        // at the same time
        $this->db = new \ezSQL_mysql('root','','softSite','localhost');
        //$this->db->trace=true;
        //$this->db->debug_all= true;
    }

    public function Save()
    {
        if (is_null($this->_id))
        {
            //project does not have an identifier therefore save as a new row in the database.
            // Insert a new project into the database..
            $this->db->query("INSERT INTO ".$this->tableName." (".$this->GetFieldString().") VALUES ('{$this->name}','{$this->description}')") ;

            // Get the id of the last inserted project.
            $this->_id = $this->db->insert_id;
        }
        else
        {
            //project has an identifier therefore update the project in the database.
            $this->db->query("UPDATE Projects SET name = '{$this->name}',description ='{$this->description}' WHERE id = {$this->_id}");
        }

    }

    private function GetFieldsString()
    {
        $classVariables = get_class_vars($this);
        $fieldString = "";

        foreach ($classVariables as $variable)
        {
            $fieldString .= "'".$variable."'";
        }

        $fieldString = str_replace("''","','",$fieldString);

        return $fieldString;
    }


    private function GetDataString()
    {

    }

}